﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

namespace Diary.Web.Blazor.WASM
{
    static public class Tools
    {
        public static string get_api_url(string biztype, string function, string pms="")
        {
            string host = "localhost:5187";
            
            if (string.IsNullOrEmpty(pms))
            {
                return string.Format("http://{0}/api?biztype={1}&function={2}", host, biztype, function);
            }
            else
            {
                return string.Format("http://{0}/api?biztype={1}&function={2}&{3}", host, biztype, function, pms);
            }
            
        }

        static public int getQueryInt(this NavigationManager navigation, string key, int dv = -1)
        {
            var uri = navigation.ToAbsoluteUri(navigation.Uri);
            string id = System.Web.HttpUtility.ParseQueryString(uri.Query).Get(key);
            if (string.IsNullOrEmpty(id)) return dv;
            return int.Parse(id);
        }

        static public string getQuery(this NavigationManager navigation, string key, string dv = "")
        {
            var uri = navigation.ToAbsoluteUri(navigation.Uri);
            string ret = System.Web.HttpUtility.ParseQueryString(uri.Query).Get(key);
            if (string.IsNullOrEmpty(ret))
            {
                ret = dv;
            }
            return ret;
        }
    }
}
